# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import os


class SinanewsPipeline:
    def process_item(self, item, spider):
        article_path = item['article_path']
        article_title = item['article_title']
        save_path = os.path.join(article_path, f'{article_title}.txt')
        with open(save_path, 'w', encoding='utf-8') as f:
            f.write(article_title)
            f.write('\n --------------------- \n')
            f.write(item['article_content'])
        return item
